﻿using Bll;
using Entity;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadTest
{
    public class DelegateThreadDemo
    {
        public class Person
        {
            public string Name;
            public int Age;
        }

        delegate int MyDelegate(VoteRecordModel vv);


        //public static void DelegateThreadMethod()
        //{
        //    ThreadMessage("Main Thread");

        //    //建立委托
        //    MyDelegate myDelegate = new MyDelegate(Hello);

        //    //建立Person对象
        //    Person person = new Person();
        //    person.Name = "Elva";
        //    person.Age = 27;

        //    //异步调用委托，输入参数对象person, 获取计算结果
        //    myDelegate.BeginInvoke("Leslie", new AsyncCallback(Completed), person);

        //    //在启动异步线程后，主线程可以继续工作而不需要等待
        //    for (int n = 0; n < 6; n++)
        //        Console.WriteLine("  Main thread do work!");
        //    Console.WriteLine("");

        //    Console.ReadKey();
        //}

        public static int DelegateThreadMethodTest(long showId, string userName, int limitVoteNum)
        {
            ThreadMessage("Main Thread");

            VoteRecordModel vv = new VoteRecordModel();
            vv.ShowId = showId;
            vv.UserName = userName;
            vv.LimitVoteNum = limitVoteNum;
            //建立委托
            MyDelegate myDelegate = new MyDelegate(Hello);

            //同步调用委托
            var result = myDelegate.BeginInvoke(vv,null, null);
            int data = myDelegate.EndInvoke(result);

            return data;
        }
        static int Hello(VoteRecordModel vm)
        {
            //投票操作
            int b = BllVoteNumber.GetResultByProceDure(vm.ShowId, vm.UserName, vm.LimitVoteNum);
            if (b==1)
            {
                VoteRecord vr = new VoteRecord();
                vr.UserName = vm.UserName;
                vr.ShowId = vm.ShowId;
                vr.CreateTime = DateTime.Now;
                BllVoteNumber.AddNewEntity<VoteRecord>("VoteRecord", vr);
                BllVoteNumber.UpdateEntityBySql("VoteShow", " update VoteShow set VoteNum=VoteNum+1 where id='" + vm.ShowId + "'");
                VoteShow vs= BllVoteNumber.SelectEntity<VoteShow>("VoteShow", vm.ShowId);
                b = vs.VoteNum;
            }

            return b;
        }

        //static void Completed(IAsyncResult result)
        //{
        //    ThreadMessage("Async Completed");

        //    //获取委托对象，调用EndInvoke方法获取运行结果
        //    AsyncResult _result = (AsyncResult)result;
        //    MyDelegate myDelegate = (MyDelegate)_result.AsyncDelegate;
        //    string data = myDelegate.EndInvoke(_result);
        //    //获取Person对象
        //    Person person = (Person)result.AsyncState;
        //    string message = person.Name + "'s age is " + person.Age.ToString();

        //    Console.WriteLine(data + "\n" + message);
        //}

        //static void Votes(IAsyncResult result)
        //{
        //    ThreadMessage("Async Completed");

        //    //获取委托对象，调用EndInvoke方法获取运行结果
        //    AsyncResult _result = (AsyncResult)result;
        //    MyDelegate myDelegate = (MyDelegate)_result.AsyncDelegate;
        //    bool data = myDelegate.EndInvoke(_result);
        //}
        static void ThreadMessage(string data)
        {
            string message = string.Format("{0}\n  ThreadId is:{1}",
                   data, Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine(message);
        }

    }
}
